#ifndef DATALOGGER_H_
#define DATALOGGER_H_

#include "ses_eeprom.h"
#include "tools/constants.h"



/* Stores temperature and CO2 values, averages, and the current stack offset. */
typedef struct {
	uint8_t temp_passes;
	uint8_t temperature[TABLE_SIZE];
	uint8_t avergTemp;

	uint8_t co2_passes;
	uint16_t co2[TABLE_SIZE];
	uint16_t avergCO2;

	uint16_t stack_offset;

} data;

data DATA;



/*
 * Initiate and set the memory ready to use.
 */
void eeprom_init();

/*
 * Save the median of the temperature and CO2 values of the last 5 seconds.
 */
void save_to_eeprom();

/*
 * Erase the data from the EEPROM.
 */
void forceInit();

/*
 * Save the current temperature into the DATA struct.
 * Called each second.
 *
 * @param temperature
 */
void put_temperature(uint8_t temperature);

/*
 * Save the current CO2 value into the DATA struct.
 * Called each second.
 *
 * @param co2
 */
void put_co2(uint16_t co2);

/*
 * Calculate the absolute address using an offset and a step.
 * This function keeps the program looping within a margin of addresses of
 * the EEPROM.
 *
 * @param offset	added to start address
 * @param step		positive or negative value to jump forth or back
 *
 * @return the absolute address of a data in the eeprom
 */
uint32_t calc_abs_addr(uint16_t offset, int step);

/*
 * Get the average of the last 8 median values of the temperature and the CO2.
 * Called each 5 seconds.
 */
void getAverageValues();



#endif /* DATALOGGER_H_ */
